package com.es.linklist;

/**
 * @author 二十
 * @since 2022/1/17 1:23 上午
 */
public class LinkedList<E> extends AbstractLinkedList<E> {
    @Override
    public void addFirst(E e) {
        addIndex(0, e);
    }

    @Override
    public void addIndex(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("error");
        }
        if (index == 0) {
            if (head == null) {
                head = new Node(e);
                size++;
                return;
            }
            head = new Node(e, head);
            size++;

        } else {

            Node p = head;
            for (int i = 0; i < index - 1; i++) {
                p = p.next;
            }
            p.next = new Node(e, p.next);
            size++;
        }

    }

    @Override
    public void addTail(E e) {
        addIndex(size, e);
    }

    @Override
    public E get(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("error");
        }
        Node p = head;
        for (int i = 0; i < index - 1; i++) {
            p = p.next;
        }
        return p.e;
    }

    @Override
    public E getFirst() {
        return get(0);
    }

    @Override
    public E getTail() {
        return get(size - 1);
    }

    @Override
    public boolean contains(E e) {
        Node p = head;
        for (int i = 0; i < size; i++) {
            if (p.next.e.equals(e)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void set(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("error");
        }

        Node p = head;
        for (int i = 0; i < index - 1; i++) {
            p = p.next;
        }
        p.next.e = e;
    }

    // 10 9  8 7 6 5 4 3 2 1
    @Override
    public void deleteNode(int index) {

        if (index < 0 || index > size) {
            throw new IllegalArgumentException("error");
        }
        if (size == 1) {
            head = null;
            size--;
            return;
        }
        Node p = head;

        for (int i = 0; i < index - 1; i++) {
            p = p.next;
        }
        p.next = p.next.next;
        size--;
    }

    @Override
    public void deleteFirst() {
        deleteNode(0);
    }

    @Override
    public void deleteLast() {
        deleteNode(size - 1);
    }

    @Override
    public void deleteElement(E e) {
        deleteElement(head, e);
    }

    private Node deleteElement(Node head, E val) {

        if (head == null) {
            return null;
        }
        Node tmp = deleteElement(head.next, val);
        if (head.e.equals(val)) {
            return tmp;
        } else {
            head.next = tmp;
            return head;
        }
    }

    @Override
    public int getSize() {
        return size;
    }

    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node node) {
            this.next = node;
            this.e = e;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return "Node{" +
                    "e=" + e +
                    '}';
        }
    }

    private int size;

    private Node head;

    public LinkedList() {

    }

    @Override
    public String toString() {
        Node p = head;
        StringBuilder sb = new StringBuilder("LinkedList:[");
        for (int i = 0; i < size; i++) {
            sb.append(p.e);
            p = p.next;
            if (i != size - 1) {
                sb.append(",");
            }
        }
        return sb.append("]").toString();
    }
}
